var CataLog=function () {

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }

    return{
        showSetupAttribute:function(id){
            $('#setupAttribute').modal({
                show:true
            });
            $('#classId').val(id);

            $('#attributeTable').dataTable().fnReloadAjax("/api/catalog/findDataAttributeByPage?classId="+id);
        },
        showAddAttribute:function(id){
            $('#addAttribute').modal({
                show:true
            });
            $('#addAttribute h4').text('编辑属性');

            $.ajax({
                url:'/api/catalog/findAttributeById',
                data:{id:id},
                type:'post',
                success:function (data) {
                    if(data.data){
                        $('#name').val(data.data.name);
                        $('#id').val(data.data.id);
                    }
                }
            })
        },
        addAttribute:function () {
            var data = $('#attributeForm').serializeObject();
            $.ajax({
                url:'/api/catalog/saveAttribute',
                data:JSON.stringify(data),
                type:'post',
                dataType:'json',
                contentType: "application/json; charset=utf-8",
                success:function (data) {
                    if(data.data){
                        $('#attributeTable').dataTable().fnDraw();
                        $('#attributeForm')[0].reset();
                        $('#addAttribute').modal("hide");
                    }
                }
            })
        },
        saveCataLog:function () {
            var data = $('#catalogForm').serializeObject();
            $.ajax({
                url:'/api/catalog/createCataLog',
                data:JSON.stringify(data),
                type:'post',
                dataType:'json',
                contentType: "application/json; charset=utf-8",
                success:function (data) {
                    if(data.data){
                        $('#catalogTable').dataTable().fnDraw();
                        $('#catalogForm')[0].reset();
                        $('#addCatalog').modal("hide");
                    }
                }
            })
        },
        deleteCatLog:function (id) {
            $.ajax({
                url:'/api/catalog/deleteCatalog',
                data:{id:id},
                type:'post',
                success:function (data) {
                    if(data.data){
                        $('#catalogTable').dataTable().fnDraw();
                    }
                }
            })
        },
        deleteAttribute:function (id) {
            $.ajax({
                url:'/api/catalog/deleteAttribute',
                data:{id:id},
                type:'post',
                success:function (data) {
                    if(data.data){
                        $('#attributeTable').dataTable().fnDraw();
                    }
                }
            })
        }
    }
}()